package com.example.demo.controller;

import com.example.demo.pojo.Result;
import com.example.demo.pojo.entity.QuestionLabel;
import com.example.demo.pojo.vo.LabelShowVO;
import com.example.demo.service.ILabelService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author fjh
 */
@RestController
@RequestMapping("/label")
@Slf4j
@Api(tags = "知识点相关接口")
@RequiredArgsConstructor
@CrossOrigin(origins = {"http://localhost:8080"}, allowCredentials = "true")
public class LabelController {
    private final ILabelService labelService;
    @ApiOperation("初始化题型")
    @PostMapping("/init")
    public Result initBank(int rootId) throws Exception {
        return Result.success(labelService.initLabel(rootId));
    }
    @ApiOperation("根据id查找题型")
    @PostMapping("/searchById")
    public Result searchLabelById(int labelId) {
        QuestionLabel questionLabel=labelService.searchLabelById(labelId);
        if(questionLabel!=null) {
            return Result.success(questionLabel);
        }
        return Result.error("不存在");
    }
    @ApiOperation("根据名字模糊查找题型")
    @PostMapping("/searchByName")
    public Result searchLabelByName(int rootId,String labelName)throws Exception {
        System.out.println("开始查找");
        System.out.println(rootId+"name is "+labelName);
        List<LabelShowVO> result =labelService.searchLabelByName(rootId,labelName);
        if(result!=null) {
            return Result.success(result);
        }
        return Result.error("不存在");
    }
    @ApiOperation("同级添加题型")
    @PostMapping("/addLabelBySameLevel")
    public Result addLabelBySameLevel(int labelId,String labelName) {
        QuestionLabel result =labelService.addLabelBySameLevel(labelId,labelName);
        if(result!=null) {
            return Result.success(result);
        }
        return Result.error("不存在");
    }
    @ApiOperation("子级添加题型")
    @PostMapping("/addLabelByChildrenLevel")
    public Result addLabelByChildrenLevel(int labelId,String labelName) {
        QuestionLabel result =labelService.addLabelByChildrenLevel(labelId,labelName);
        if(result!=null) {
            return Result.success(result);
        }
        return Result.error("不存在");
    }
    @ApiOperation("查找所以知识点的Id表")
    @PostMapping("/getLabelId")
    public Result getLabelId(int subjectId) {
        List<Integer> result =labelService.getLabelId(subjectId);
        if(result!=null) {
            return Result.success(result);
        }
        return Result.error("不存在");
    }
}
